var obj1 = {
  name: "zhangsan",
  age: 22,
  language: [1, [2, 3], [4, 5]],
};
var obj11 = {
  name: "zhangsan",
  age: 22,
  language: [1, [2, 3], [4, 5]],
};
var obj111 = {
  name: "zhangsan",
  age: 22,
  language: [1, [2, 3], [4, 5]],
};

//赋值
var obj2 = obj1;
obj2.name = "lisi";
obj2.language[1] = ["二", "三"];
console.log("obj1", obj1);
console.log("obj2", obj2);

//浅拷贝
var obj3 = shallowCopy(obj11);
obj3.name = "lisi";
obj3.language[1] = ["二", "三"];
function shallowCopy(src) {
  var dst = {};
  for (var prop in src) {
    if (src.hasOwnProperty(prop)) {
      //hasOwnProperty检测prop是否为src的自有属性（类似于一级属性），
      dst[prop] = src[prop];
    }
  }
  return dst;
}
console.log("obj11", obj11);
console.log("obj3", obj3);

//深拷贝
var obj4 = deepClone(obj111);
obj4.name = "lisi";
obj4.language[1] = ["二", "三"];
function deepClone(startObj, endObj) {
  let obj = endObj || {};
  for (let i in startObj) {
    if (typeof startObj[i] === "object") {
      //constructor构造函数
      startObj[i].constructor === Array ? (obj[i] = []) : (obj[i] = {});
      deepClone(startObj[i], obj[i]);
    } else {
      obj[i] = startObj[i];
    }
  }
  return obj;
}
console.log("obj111", obj111);
console.log("obj4", obj4);
// console.log(obj1.constructor);//[Function: Object]
